package fgame.unit
{
	import fgame.action.Act3D;
	import fgame.action.ActUtils;
	import fgame.action.WaitAct;
	import fgame.graphic.Graphic;
	
	import flare.collisions.SphereCollision;
	import flare.core.Pivot3D;
	
	import flash.geom.Vector3D;
	import flash.utils.Dictionary;
	
	import ricky.Interface.IGraphic;
	import ricky.game.Actor;
	
	public class Actor3D extends Actor
	{
		public function Actor3D()
		{
			defaultAct3d=new WaitAct(this)
			_act=defaultAct3d;
		}
		
		public function get actList():Vector.<Act3D>
		{
			if(!_actList)
				_actList=new Vector.<Act3D>(ActUtils.acts.length);
			return _actList;
		}

		/**
		 * 当前移动速率
		 */			
		public function get speed():Number
		{
			return _speed;
		}

		public function set speed(value:Number):void
		{
			_speed = value;
		}

		/**
		 * 网络延时
		 */
		public function get netDelay():Number
		{
			return _netDelay;
		}

		public function set netDelay(value:Number):void
		{
			_netDelay = value;
		}

		/**
		 * 播放动画行动
		 */
		override public function playAnimAct(act:String=null):void
		{
			if(act!=null)
				_currAnimAct=act;
			
			playanimactbyname(_currAnimAct);
			
		}
		
		override public function get graphic():IGraphic
		{
			if(!_graphic){
				_graphic=new Graphic(this);
			}
			return _graphic
		}
		
		///////////////////////////////////////////////////////////////////////////
		//****************************公共方法*************************************
		///////////////////////////////////////////////////////////////////////////
		
		override public function processAct(type:int, args:*):void
		{
			Debug.traceObj(null,args,this._name,"收到act"+type,0x0f00f0);
			
			if(act.types==type)
			{
				act.purpose(args);
			}
			else
			{
				this.act.end();
				var actTmp:Act3D=getActFormCach(type);
				actTmp.purpose(args);
				actTmp.begin();
				this._act=actTmp;
			}
			
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////
		//****************************受保护的方法*************************************
		///////////////////////////////////////////////////////////////////////////
		
		/**
		 * 行动结束响应函数
		 */ 
		protected function actComplete(e:Act3D):void
		{
			e.end();
			
			if(!defaultAct)
			{
				defaultAct=new WaitAct(this);
				defaultAct.actor=this;
//				defaultAct.completeFun=actComplete
			}
			
			defaultAct.begin();
			this._act=defaultAct;
		}
		
		protected function getActFormCach(type:int):Act3D
		{
			if(this.actList.length>type)
			{
				var act3d:Act3D=actList[type];
				if(act3d==null)
				{
					var actclass:Class=ActUtils.getActClassByType(type);
					if(actclass)
					{
						act3d=new actclass();
						act3d.actor=this;
						act3d.completeFun=actComplete;
						_actList[type]=act3d;
						return act3d;
					}
				}else
					return act3d;
			}
			
			return null;
		}
		
		
		///////////////////////////////////////////////////////////////////////////
		//****************************公共属性*************************************
		///////////////////////////////////////////////////////////////////////////
		
		/**
		 * 球形碰撞检测
		 */
		public function get sphereCollision():SphereCollision
		{
			if(!_sphereCollision)
				_sphereCollision=new SphereCollision(Pivot3D(graphic),3,new Vector3D(0,3,0));
			return _sphereCollision;
		}
		
		
		/**
		 * 或者是Z坐标
		 */
		override public function get gridY():int
		{ 
			_gridY=this.graphic.z/Common.tileHeight;
			return _gridY;
		}
		
		override public function set gridY(value:int):void
		{
			this.graphic.z = value*Common.tileHeight;
			_gridY=value;
		}
		
		//////////////////////////////////////////////////////////////////////////
		//*************************** private ***********************************
		//////////////////////////////////////////////////////////////////////////
		
		private var _sphereCollision:SphereCollision;
		private var _actList:Vector.<Act3D>;
		public var defaultAct3d:Act3D;
		private var _netDelay:Number=0;
		private var _speed:Number=0;//

		public var  defaultAct:Act3D;
	}
}